package org.jiajie.coffeecodes.ws;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author jie
 */
public class TestApp {
    public static void main(String[] args) {
        try {
            // open websocket
            final WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint(new URI("ws://service.jagachina.com:41708/echo"));

            // add listener
            clientEndPoint.addMessageHandler(new WebsocketClientEndpoint.MessageHandler() {
                public void handleMessage(String message) {
                    System.out.println(message);
                }
            });

            // send message to websocket
            clientEndPoint.sendMessage("{\"sn\":\"BC00D09B050898B5\",\"cmd\":\"userreg\",\"data\":{\"userid\":\"\",\"key\":\"88888888\"}}");
            clientEndPoint.sendMessage("{\"sn\":\"BC00D09B050898B5\",\"cmd\":\"control\",\"data\":\"EIS5=14/0/23=230\"}");

            // wait 5 seconds for messages from websocket
            Thread.sleep(5000);

        } catch (InterruptedException ex) {
            System.err.println("InterruptedException exception: " + ex.getMessage());
        } catch (URISyntaxException ex) {
            System.err.println("URISyntaxException exception: " + ex.getMessage());
        }
    }
}
